package com.pheruagor.handmanager.view {
    import com.pheruagor.handmanager.controller.CloseClubCreationPopUpCommand;
    import com.pheruagor.handmanager.controller.CloseStartingPopUpCommand;
    import com.pheruagor.handmanager.controller.OpenClubCreationPopUpCommand;
    import com.pheruagor.handmanager.model.ApplicationProxy;
    import com.pheruagor.handmanager.model.business.Club;
    import com.pheruagor.handmanager.model.business.event.ValidationEvent;
    import com.pheruagor.handmanager.view.component.StartingPopUp;
    import com.pheruagor.handmanager.view.component.ValidationPopUp;

    import flash.events.MouseEvent;

    import mx.controls.Alert;
    import mx.events.FlexEvent;
    import mx.managers.PopUpManager;
    import mx.utils.StringUtil;

    import org.puremvc.as3.interfaces.INotification;
    import org.puremvc.as3.patterns.mediator.Mediator;

    public class StartingPopupMediator extends Mediator {
        public static const NAME:String = "startingPopupMediator";

        //-----------------------------------------------------
        // Initialization
        //-----------------------------------------------------
        public function StartingPopupMediator(view:StartingPopUp) {
            super(NAME, view);
        }

        //-----------------------------------------------------
        // Properties
        //-----------------------------------------------------
        public function get startingPopUp():StartingPopUp {
            return StartingPopUp(viewComponent);
        }

        //-----------------------------------------------------
        // IMediator Interface
        //-----------------------------------------------------
        override public function onRegister():void {
            startingPopUp.addEventListener(FlexEvent.CREATION_COMPLETE, onStartingPopUpCompleted);
        }

        override public function onRemove():void {
            startingPopUp.removeEventListener(FlexEvent.CREATION_COMPLETE, onStartingPopUpCompleted);
            startingPopUp.newClubButton.removeEventListener(MouseEvent.CLICK, onNewClubButtonClicked);
            startingPopUp.cancelButton.removeEventListener(MouseEvent.CLICK, onCancelButtonClicked);
            startingPopUp.okButton.removeEventListener(MouseEvent.CLICK, onOkButtonClicked);

        }

        override public function listNotificationInterests():Array {
            return [CloseClubCreationPopUpCommand.EXECUTED];
        }

        override public function handleNotification(notification:INotification):void {
            switch (notification.getName()) {
                case CloseClubCreationPopUpCommand.EXECUTED:

                    var club:Club = Club(notification.getBody());
                    var proxy:ApplicationProxy = ApplicationProxy(facade.retrieveProxy(ApplicationProxy.NAME));

                    if (club != null) {
                        proxy.addClub(club);
                    }
                    startingPopUp.clubList.dataProvider = proxy.clubs;
                    startingPopUp.clubList.selectedItem = club;
                    break;
            }
        }

        //-----------------------------------------------------
        // Event handler
        //-----------------------------------------------------
        public function onStartingPopUpCompleted(event:FlexEvent):void {
            startingPopUp.okButton.addEventListener(MouseEvent.CLICK, onOkButtonClicked);
            startingPopUp.cancelButton.addEventListener(MouseEvent.CLICK, onCancelButtonClicked);
            startingPopUp.newClubButton.addEventListener(MouseEvent.CLICK, onNewClubButtonClicked);
            startingPopUp.removeClubButton.addEventListener(MouseEvent.CLICK, onRemoveButtonClicked);
            buildView();
        }

        private function onOkButtonClicked(event:MouseEvent):void {
            if (startingPopUp.clubList.selectedItem == null) {
                Alert.show("Veuillez sélectionner un club");
                return;
            }
            var club:Club = Club(startingPopUp.clubList.selectedItem);
            sendNotification(CloseStartingPopUpCommand.NAME, {"popup": startingPopUp, "club": club});
        }

        private function onCancelButtonClicked(event:MouseEvent):void {
            sendNotification(CloseStartingPopUpCommand.NAME, {"popup": startingPopUp, "club": null});
        }

        private function onNewClubButtonClicked(event:MouseEvent):void {
            sendNotification(OpenClubCreationPopUpCommand.NAME, startingPopUp);
        }

        private function onRemoveButtonClicked(event:MouseEvent):void {
            if (startingPopUp.clubList.selectedItem == null) {
                Alert.show("Vous devez dabords sélectionner un club.");
                return;
            }
            var club:Club = Club(startingPopUp.clubList.selectedItem);
            var popup:ValidationPopUp = new ValidationPopUp();
            var mediator:ValidationPopUpMediator = new ValidationPopUpMediator(popup);
            facade.registerMediator(mediator);
            PopUpManager.addPopUp(popup, startingPopUp);
            PopUpManager.centerPopUp(popup);
            mediator.validationTitle = "Suppression d'un club";
            mediator.validationLabel = StringUtil.substitute("Etes vous sur de vouloir supprimer le club '{0}'?", club.name);
            popup.addEventListener(ValidationEvent.OK, onValidationOk);
            popup.addEventListener(ValidationEvent.CANCEL, onValidationCancel);
        }

        private function onValidationOk(event:ValidationEvent):void {
            var proxy:ApplicationProxy = ApplicationProxy(facade.retrieveProxy(ApplicationProxy.NAME));
            proxy.removeClub(Club(startingPopUp.clubList.selectedItem));
            removeValidationPopUp();
        }

        private function onValidationCancel(event:ValidationEvent):void {
            removeValidationPopUp();
        }

        private function removeValidationPopUp():void {
            var mediator:ValidationPopUpMediator = ValidationPopUpMediator(facade.retrieveMediator(ValidationPopUpMediator.NAME));
            mediator.validationPopUp.removeEventListener(ValidationEvent.OK, onValidationOk);
            mediator.validationPopUp.removeEventListener(ValidationEvent.CANCEL, onValidationCancel);
            PopUpManager.removePopUp(mediator.validationPopUp);
            facade.removeMediator(ValidationPopUpMediator.NAME);
        }

        //-----------------------------------------------------
        // Implementation
        //-----------------------------------------------------
        public function buildView():void {
            var proxy:ApplicationProxy = ApplicationProxy(facade.retrieveProxy(ApplicationProxy.NAME));
            startingPopUp.clubList.dataProvider = proxy.clubs;
        }

    }
}